<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="adminSysNotice" :rules="rules" ref="AdminSysNoticeFrom" label-width="150px">
      <el-form-item label="标题" prop="emailTitle">
        <el-input v-model="adminSysNotice.emailTitle" style="width: 370px;"/>
      </el-form-item>

      <el-form-item label="内容" prop="emailDetail">
        <el-input v-model="adminSysNotice.emailDetail" type="textarea" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="会员" prop="memberIds">
        <el-table :data="mlist" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="60" align="center"></el-table-column>
          <el-table-column prop="nickname" label="会员昵称" width="180"></el-table-column>
          <el-table-column prop="phone" label="手机号" width="180"></el-table-column>
          <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
          <el-table-column prop="level" label="等级"></el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('AdminSysNoticeFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('AdminSysNoticeFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import {
  createEmail,
  getAdminSysNotice,
  updateAdminSysNotice,
  getDetail
} from "@/api/sys/adminSysNotice";
import { fetchList } from "@/api/ums/member";
import SingleUpload from "@/components/Upload/singleUpload";

const defaultAdminSysNotice = {
  emailTitle: "",
  emailDetail: "",
  memberIds: ""
};
import axios from 'axios'
const QS  = require('qs');
export default {
  name: "AdminSysNoticeDetail",
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      adminSysNotice: Object.assign({}, defaultAdminSysNotice),
      rules: {
        emailTitle: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ],
        emailDetail: [
          { required: true, message: "请输入内容", trigger: "blur" }
        ]
      },
      mlist: []
    };
  },
  created() {
    this.getMemberList();
    if (this.isEdit) {
      getDetail(this.$route.query.id).then(response => {
        this.adminSysNotice = response.data;
      });
    } else {
      this.adminSysNotice = Object.assign({}, defaultAdminSysNotice);
    }
  },
  methods: {
    handleSelectionChange(e) {
      console.log(e, 999);
      let arr = [];
      e.map(v => {
        arr.push(v.id);
      });
      this.adminSysNotice.memberIds = arr.join(",");
    },
    getMemberList() {
      fetchList({ pageNum: 1, pageSize: 10000 }).then(res => {
        console.log(res, 999);
        let flist = res.data.records.filter(v => v.email)
        this.mlist = flist;

      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if (this.isEdit) {
                  axios.post(process.env.BASE_API + 'cmsEmail/saveOrUpdate', QS.stringify(this.adminSysNotice),
                  ).then(res => {
                    this.$refs[formName].resetFields();
                    this.topic = Object.assign({},defaultAdminSysNotice);
                    this.$message({
                        message: '添加成功',
                        type: 'success',
                        duration:1000
                    });
                this.$router.back();
                  }).catch(err => {
                      console.log(err,888)
                  })
            //   createEmail(this.$route.query.id, this.adminSysNotice).then(
            //     response => {
            //       if (response.code == 200) {
            //         this.$refs[formName].resetFields();
            //         this.$message({
            //           message: "修改成功",
            //           type: "success",
            //           duration: 1000
            //         });
            //         this.$router.back();
            //       } else {
            //         this.$message({
            //           message: response.msg,
            //           type: "error",
            //           duration: 1000
            //         });
            //       }
            //     }
            //   );
            } else {
                  axios.post(process.env.BASE_API + 'cmsEmail/saveOrUpdate', QS.stringify(this.adminSysNotice),
                  ).then(res => {
                    this.$refs[formName].resetFields();
                    this.topic = Object.assign({},defaultAdminSysNotice);
                    this.$message({
                        message: '添加成功',
                        type: 'success',
                        duration:1000
                    });
                this.$router.back();
                  }).catch(err => {
                      console.log(err,888)
                  })
            //   createEmail(this.adminSysNotice).then(response => {
            //     if (response.code == 200) {
            //       this.$refs[formName].resetFields();
            //       this.adminSysNotice = Object.assign(
            //         {},
            //         defaultAdminSysNotice
            //       );
            //       this.$message({
            //         message: "提交成功",
            //         type: "success",
            //         duration: 1000
            //       });
            //       this.$router.back();
            //     } else {
            //       this.$message({
            //         message: response.msg,
            //         type: "error",
            //         duration: 1000
            //       });
            //     }
            //   });
            }
          });
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.adminSysNotice = Object.assign({}, defaultAdminSysNotice);
    }
  }
};
</script>
<style>
</style>


